<?php
/**
 * //w/w/w.s/h/u/o/t//u/p/u c/o/m  说/图/谱/源/码/分/享
 */
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/24 0024
 * Time: 下午 1:59
 */
defined("IN_IA")or exit("Access Denied");
checklogin();  //验证是否登录
global $_W,$_GPC;
$uniacid=$_W['uniacid'];
$op=$_GPC['op'] ? $_GPC['op'] : "send_list";

//种子列表
if($op=='send_list'){
    $condition=array();
    if($_GPC['send_name']){
        $send_name=$_GPC['send_name'];
        $condition['send_name LIKE']='%'.$send_name.'%';
    }
    $condition['uniacid']=$uniacid;
    $listCount=pdo_getall("cqkundian_farm_send",$condition);
    $total=count($listCount);   //数据的总条数
    $pageSize=10; //每页显示的数据条数
    $pageIndex=intval($_GPC['page']) ? intval($_GPC['page']) :1;  //当前页
    $pager=pagination($total,$pageIndex,$pageSize);
    $list=pdo_getall("cqkundian_farm_send",$condition,'','','rank asc',array($pageIndex,$pageSize));
    include $this->template('web/land/send_list');
}

//编辑新增种子
if($op=='send_edit'){
    if($_GPC['id']){
        $list=pdo_get('cqkundian_farm_send',array('id'=>$_GPC['id'],'uniacid'=>$uniacid));
    }
    include $this->template('web/land/send_edit');
}

//保存种子
if($op=='send_save'){
    $data=array(
        'send_name'=>$_GPC['send_name'],
        'rank'=>$_GPC['rank'],
        'cover'=>tomedia($_GPC['cover']),
        'price'=>$_GPC['price'],
        'output'=>$_GPC['output'],
        'effect'=>$_GPC['effect'],
        'send_time'=>$_GPC['send_time'],
        'cycle'=>$_GPC['cycle'],
        'is_putaway'=>$_GPC['is_putaway'],
        'uniacid'=>$uniacid,
    );
    $slide=$_GPC['send_slide'];
    for($i=0;$i<count($slide);$i++){
        $slide[$i]=tomedia($slide[$i]);
    }
    $data['send_slide']=serialize($slide);
    if(empty($_GPC['id'])){  //新增
        $request=pdo_insert("cqkundian_farm_send",$data);
    }else{
        $condition=array(
            'id'=>$_GPC['id'],
            'uniacid'=>$uniacid,
        );
        $request=pdo_update("cqkundian_farm_send",$data,$condition);
    }
    if($request){
        message("操作成功",$this->createWebUrl("send"));
    }else {
        message("操作失败", '', 'warning');
    }
}

//上架/下架
if($op=='is_putaway'){
    $id=$_GPC['id'];
    $res=pdo_update('cqkundian_farm_send',array('is_putaway'=>$_GPC['status']),array('id'=>$id,'uniacid'=>$uniacid));
    echo $res ? json_encode(array('status'=>1)) : json_encode(array('status'=>2));die;
}

//删除
if($op=='send_delete'){
    $id=$_GPC['id'];
    $res=pdo_delete('cqkundian_farm_send',array('id'=>$id,'uniacid'=>$uniacid));
    echo $res ? json_encode(array('status'=>1)) : json_encode(array('status'=>2));die;
}

//种植列表
if($op=='send_plant'){
    $formLandData=pdo_getall('cqkundian_farm_land',array('uniacid'=>$uniacid));
    $condition=array();
    if(!empty($_GPC['lid'])){
        $lid=$_GPC['lid'];
        $condition['lid']=$_GPC['lid'];
    }
    $status=$_GPC['status'];
    if($status!=4 && $status){
        if($status==3){
            $condition['status']=0;
        }else{
            $condition['status']=$status;
        }
    }
    $condition['uniacid']=$uniacid;
    if(empty($_GPC['name'])) {
        $all = pdo_getall('cqkundian_farm_land_mine', $condition);
        $count = count($all);
        $pageSize = 18;
        $pageIndex = $_GPC['page'] ? intval($_GPC['page']) : 1;
        $pager = pagination($count, $pageIndex, $pageSize);
        $list = pdo_getall('cqkundian_farm_land_mine', $condition, '', '', 'create_time desc', array($pageIndex, $pageSize));
    }else{
        $name=trim($_GPC['name']);
        $user_where=array(
            'uniacid'=>$uniacid,
            'nickname LIKE'=>'%'.$name.'%',
        );
        $userData=pdo_getall('cqkundian_farm_user',$user_where);
        $uid_arr=array();
        for($i=0;$i<count($userData);$i++){
            $uid_arr[]=$userData[$i]['uid'];
        }
        $condition['uid in']=$uid_arr;
        $list = pdo_getall('cqkundian_farm_land_mine', $condition, '', '', 'create_time desc');
    }
    for ($i = 0; $i < count($list); $i++) {
        $list[$i]['seed_area'] = 0;
        $userData = pdo_get('cqkundian_farm_user', array('uid' => $list[$i]['uid'], 'uniacid' => $uniacid));
        $landData = pdo_get('cqkundian_farm_land', array('id' => $list[$i]['lid'], 'uniacid' => $uniacid));
        $landOrder=pdo_get('cqkundian_farm_land_order',array('id'=>$list[$i]['order_id'],'uniacid'=>$uniacid));
        $list[$i]['truename']=$landOrder['username'];
        $list[$i]['nickname'] = $userData['nickname'];
        $list[$i]['land_name'] = $landData['land_name'];
        $land_mine = pdo_getall('cqkundian_farm_send_mine', array('lid' => $list[$i]['id'], 'uniacid' => $uniacid));
        for ($j = 0; $j < count($land_mine); $j++) {
            $list[$i]['seed_area'] += $land_mine[$j]['count'];
            if ($land_mine[$j]['status'] == 0) {
                $list[$i]['is_plant'] = 1;
                $list[$i]['seedData'][]=$land_mine[$j];
            }
        }

        //判断是否过期
        if($list[$i]['exprie_time'] < time()){  //过期
            pdo_update('cqkundian_farm_land_mine',array('status'=>2),array('uniacid'=>$uniacid,'id'=>$list[$i]['id']));
            $list[$i]['is_expire'] = '到期';
        }else {
            if ($list[$i]['status'] == 2) {
                $list[$i]['is_expire'] = '到期';
            } else {
                $list[$i]['is_expire'] = '未到期';
            }
        }
    }
    include $this->template("web/seed/seed_plant");
}

//种植详情
if($op=='seed_plant_detail'){
    $id=$_GPC['id'];
    $landMime=pdo_get('cqkundian_farm_land_mine',array('id'=>$id,'uniacid'=>$uniacid));
    $landMime['exprie_time']=date("Y-m-d H:i:s",$landMime['exprie_time']);
    $landOrder=pdo_get('cqkundian_farm_land_order',array('id'=>$landMime['order_id'],'uniacid'=>$uniacid));
    $land=pdo_get('cqkundian_farm_land',array('uniacid'=>$uniacid,'id'=>$landMime['lid']));
    $user=pdo_get('cqkundian_farm_user',array('uid'=>$landMime['uid'],'uniacid'=>$uniacid));

    $send_mine = pdo_getall('cqkundian_farm_send_mine', array('lid' => $landMime['id'], 'uniacid' => $uniacid));
    include $this->template("web/seed/seed_plant_detail");
}

//保存物联网设备
if($op=='save_web_did'){
    $id=$_GPC['id'];
    $web_did=$_GPC['web_did'];
    $res=pdo_update('cqkundian_farm_land_mine',array('web_did'=>$web_did),array('uniacid'=>$uniacid,'id'=>$id));
    if($res){
        message('保存成功',url('site/entry/send',array('m'=>'kundian_farm','op'=>'send_plant')));die;
    }else{
        message('操作失败');die;
    }
}


//种植种子详情
if($op=='seed_detail'){
    $id=$_GPC['id'];
    $list=pdo_getall('cqkundian_farm_send_mine',array('lid'=>$id,'uniacid'=>$uniacid));
    for ($i=0;$i<count($list);$i++){
        $seedData=pdo_get('cqkundian_farm_send',array('id'=>$list[$i]['sid'],'uniacid'=>$uniacid));
        $list[$i]['cover']=$seedData['cover'];
        if($list[$i]['status']==1 || $list[$i]['status']==2){
            $day=floor((time()-$list[$i]['seed_time'])/60/60/24);
            $list[$i]['day']=$day;
            pdo_update('cqkundian_farm_send_mine',array('day'=>$day),array('id'=>$list[$i]['id'],'uniacid'=>$uniacid));
        }
        $list[$i]['expect_time']=date("Y-m-d",$list[$i]['expect_time']);
    }
    include $this->template("web/seed/seed_list");
}

//种植状态改变
if($op=='changeSeedStatus'){
    $id=$_GPC['id'];
    $status=$_GPC['status'];
    $update_data=array(
        'status'=>$status,
    );
    if($status==1){
        $update_data['seed_time']=time();
    }
    $res=pdo_update('cqkundian_farm_send_mine',$update_data,array('id'=>$id,'uniacid'=>$uniacid));
    if($res){
        echo json_encode(array('status'=>1));die;
    }else{
        echo json_encode(array('status'=>2));die;
    }
}


//状态列表
if($op=='status_list'){
    $lid=$_GPC['id'];
    $all=pdo_getall('cqkundian_farm_send_status',array('uniacid'=>$uniacid,'lid'=>$lid));
    $count=count($all);
    $pageSize=10;
    $pageIndex=$_GPC['page'] ? intval($_GPC['page']) : $_GPC['page'];
    $pager=pagination($count,$pageSize,$pageIndex);
    $list=pdo_getall('cqkundian_farm_send_status',array('uniacid'=>$uniacid,'lid'=>$lid),'','','create_time desc',array($pageIndex,$pageSize));
    for ($i=0;$i<count($list);$i++){
        $list[$i]['src']=unserialize($list[$i]['src']);
    }
    include $this->template('web/seed/status_list');
}

//状态编辑
if($op=='status_edit'){
    if(!empty($_GPC['id'])){
        $list=pdo_get('cqkundian_farm_send_status',array('id'=>$_GPC['id'],'uniacid'=>$uniacid));
        $lid=$list['lid'];
        $list['src']=unserialize($list['src']);
    }else{
        $lid=$_GPC['lid'];
    }
    include $this->template('web/seed/status_edit');
}

//保存跟踪信息
if($op=='status_save'){
    $update_data=array(
        'txt'=>$_GPC['txt'],
        'uniacid'=>$uniacid,
        'create_time'=>time(),
        'lid'=>$_GPC['lid'],
    );
    $src=$_GPC['src'];
    for($i=0;$i<count($src);$i++){
        $src[$i]=tomedia($src[$i]);
    }
    $update_data['src']=serialize($src);
    if(empty($_GPC['id'])){
        $res=pdo_insert('cqkundian_farm_send_status',$update_data);
    }else{
        $res=pdo_update('cqkundian_farm_send_status',$update_data,array('id'=>$_GPC['id'],'uniacid'=>$uniacid));
    }
    if($res){
        message('操作成功',$this->createWebUrl('send'));die;
    }else{
        message('操作失败',$this->createWebUrl('send'));die;
    }
}

//删除跟踪信息
if($op=='delete_status'){
    $id=$_GPC['id'];
    $res=pdo_delete('cqkundian_farm_send_status',array('id'=>$id,'uniacid'=>$uniacid));
    echo $res ? json_encode(array('status'=>1)) : json_encode(array('status'=>2));die;
}

//土地过期
if($op=='seed_expire'){
    $id=$_GPC['id'];
    $res=pdo_update('cqkundian_farm_land_mine',array('status'=>2),array('id'=>$id,'uniacid'=>$uniacid));
    if($res){
       echo json_encode(array('status'=>1));die;
    }else {
        echo json_encode(array('status'=>2));die;
    }
}

//修改预计成熟时间
if($op=='expect_time'){
    $id=$_GPC['id'];
    $expect_time=strtotime($_GPC['expect_time']);
    $res=pdo_update('cqkundian_farm_send_mine',array('expect_time'=>$expect_time),array('id'=>$id,'uniacid'=>$uniacid));
    echo $res ? json_encode(array('status'=>1)) : json_encode(array('status'=>2));die;
}




















